gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576
Open
NaveenKumarG-dev wants to merge 5 commits into
Open
gh-151575: Deprecate passing file paths instead of URLs to mimetypes.guess_type()#151576NaveenKumarG-dev wants to merge 5 commits into
NaveenKumarG-dev wants to merge 5 commits into
Conversation
Documentation build overview
12 files changed ·
|
…ess_type with new guess_file_type method
…com/NaveenKumarG-dev/cpython into gh-mimetypes-guess-type-deprecation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
mimetypes.guess_type()is intended for URLs, whilemimetypes.guess_file_type()was introduced in Python 3.13 as the preferred API for file paths.The implementation still accepts file paths without warning, despite the existing
# TODO: Deprecate accepting file paths (in particular path-like objects)comment inLib/mimetypes.py. This PR implements that TODO by deprecating file path inputs toguess_type()and directing users toguess_file_type()instead.Changes Made
Implementation: Modified
mimetypes.MimeTypes.guess_type()to emit aDeprecationWarning(stacklevel=2) when called with a file path instead of a URL.Coverage: The warning triggers for plain strings that do not contain a valid URL scheme, as well as for
bytesandos.PathLikeobjects. Valid URLs such ashttp://,ftp://, anddata:continue to behave as before.Testing: Added
GuessTypeDeprecationTestCasetoLib/test/test_mimetypes.pycovering path and URL inputs to verify warning behavior and prevent regressions.Documentation:
Doc/library/mimetypes.rstto document the runtime deprecation.Doc/whatsnew/3.16.rstandDoc/deprecations/pending-removal-in-future.rst.NEWS: Added the required NEWS entry using
blurb.Rationale
This change aligns the runtime behavior with the documented distinction between URL handling (
guess_type()) and file-path handling (guess_file_type()), while implementing the existing deprecation TODO in the source code.Linked Issue
Fixes #151575
Code of Conduct and CLA
mimetypes.guess_type()#151575